$(function () {
    layui.use(['form','notice'], function(){
        var form = layui.form;
        var notice = layui.notice;

        //查询当前用户信息
        function setUserInfo() {
            searchUserInfo().then(value => {
                if(value.code == 200){
                    $('#username').html(value.data.name)
                    getusermenu(value.data)
                }else{
                    notice.error('查询用户信息失败')
                    location.href = './login.html'
                }
            }).catch(reason => {
                notice.error('服务器连接失败')
                location.href = './login.html'
            })

        }
        setUserInfo()
        function getusermenu(uinfo) {
            new Promise((resolve, reject) => {
                layui.data('user', {
                    key: 'info',
                    value: uinfo
                })
                resolve(uinfo)
            }).then(value => {
                let uinfo = layui.data('user').info
                if(uinfo.uname == 'admin'){
                    $("[name='authox']").css('display','inline');
                }else{
                    searchMenusOfUser(uinfo.id).then(value1 => {
                        if(value1.code == 200){
                            layui.data('user', {
                                key: 'autho',
                                value: value1.data
                            })
                            value1.data.forEach(um=>{
                                $('#'+um.menuInfo.menuUrl).css('display','inline');
                                if(um.menuInfo.menuUrl=='agency' || um.menuInfo.menuUrl=='group'
                                    || um.menuInfo.menuUrl=='role' || um.menuInfo.menuUrl=='user'
                                    || um.menuInfo.menuUrl=='pro' || um.menuInfo.menuUrl=='autho'){
                                    $('#base').css('display','inline');
                                    if(um.menuInfo.menuUrl=='pro' || um.menuInfo.menuUrl=='autho'){
                                        if(uinfo.uname == 'admin'){
                                            $('#authom').css('display','inline');
                                        }
                                    }
                                }
                                if(um.menuInfo.menuUrl=='userautho' || um.menuInfo.menuUrl=='groupautho'
                                    || um.menuInfo.menuUrl=='roleautho' ){
                                    $('#xautho').css('display','inline');
                                }
                            })
                        }else{
                            notice.error('获取用户权限失败,'+value1.msg)
                        }
                    }).catch(reason => {
                        notice.error('服务器连接失败')
                    })
                }
            }).catch(reason => {
                notice.error('获取用户权限失败，服务器连接失败')
            })
        }
    });
})